package club.banyuan;

import club.banyuan.util.Constant;
import club.banyuan.util.PropUtil;
import com.alibaba.fastjson.JSONObject;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class PositionService {
    private int idCount = 1;
    private List<Position> positionList = new ArrayList<>();
    private static final PositionService INSTANCE = load();

    private PositionService() {
    }


    public int getIdCount() {
        return idCount;
    }

    public void setIdCount(int idCount) {
        this.idCount = idCount;
    }

    public List<Position> getPositionList() {
        return positionList;
    }

    public void setPositionList(List<Position> positionList) {
        this.positionList = positionList;
    }

    public static PositionService getINSTANCE() {
        return INSTANCE;
    }

    public void addPosition(Position position) {
        Optional<Position> any = positionList.stream().filter(t -> t.getName().equals(position.getName()))
                .findAny();

        if (any.isPresent()) {
            throw new BadRequestException("部门已存在");
        }

        position.setId(idCount++);
        positionList.add(position);
        save();
    }

    public void deletePosition(int id) {
        positionList.removeIf(position -> position.getId() == id);
        save();
    }

    public Position getPosition(int id) {
        for (Position position : positionList) {
            if (position.getId() == id) {
                return position;
            }
        }
        throw new BadRequestException("部门不存在 id:" + id);
    }

    public void updatePosition(Position position) {
        Position old = getPosition(position.getId());
        old.setName(position.getName());
        old.setDescription(position.getDescription());
        save();
    }

    private void save() {
        String rlt = JSONObject.toJSONString(this);
        try (FileOutputStream fileOutputStream = new FileOutputStream(
                PropUtil.getProp(Constant.Prop.POSITION_STORE))) {
            fileOutputStream.write(rlt.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static PositionService load() {
        try (FileInputStream inputStream = new FileInputStream(PropUtil.getProp(Constant.Prop.POSITION_STORE))) {

            return JSONObject.parseObject(inputStream, StandardCharsets.UTF_8, PositionService.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return new PositionService();
    }

    public List<Position> getRowsList(int page,int rows,String name){
        List<Position> searchList;
        if (name==null||name.equals("")){
            searchList=getPositionList();
        }else{
            searchList=positionList.stream().filter(t->
                    t.getName().contains(name)).collect(Collectors.toList());
        }

        int total=searchList.size();
        List<Position> rowsList=new ArrayList<>();

        int allPages = (int) Math.ceil(total * 1.0 / rows);
        System.out.println(allPages);
        PositionService positionService = HttpServer.getPositionService();
        if (allPages <= 1) {
            return searchList;
        }
        for (int i = 8 * (page - 1), j = 0; i < positionService.getPositionList().size() && j < 8; i++,j++) {
            rowsList.add(positionService.getPositionList().get(i));
        }
        return rowsList;
    }

    public static void main(String[] args) {
        PositionService positionService = new PositionService();
        Position position = new Position();
        position.setName("员工");
        position.setDescription("职员");
        positionService.addPosition(position);

        Position position1 = new Position();
        position1.setName("组长");
        position1.setDescription("管理职员");
        positionService.addPosition(position1);

        Position position2 = new Position();
        position2.setName("部长");
        position2.setDescription("管理组长");
        positionService.addPosition(position2);

    }
}
